$(function () {
    obj.init();

});

var obj = new function () {
    'use strict';
    return {
        dataGrid:null,
        titleRow:null,
        tabRow:null,
        init:function () {
            this.titleRow = $('#titleRow');
            this.tabRow   = $('#tabRow');
            this.dataGrid = new xhDataGrid($('#list'), 'lm/rent/getHouse',{rent:2});
            this.dataGrid.parseToolbar().init().activeButton([], ['bt_list_del','bt_list_unRent']);
            this.funObj().bindDataGridEvent();
            this.listener();
        },
        listener:function () {
            var $this = this;

            //新增事件按钮
            $('#bt_list_add').on('click',function () {
                $this.funObj().openAddForm();
            });

            $('#bt_list_del').on('click',function () {
                $this.funObj().askDelQuestion();
            });

            $('#tabReturn').on('click',function () {
                $this.funObj().goToList();
            });
            $this.funObj().bindArea();//省县市三级联动

            $('#bt_list_search').click(function () {
               $this.funObj().search();
            });

            $('#bt_list_unRent').click(function () {
                $this.funObj().setUnRent();
            })
        },
        funObj:function () {
            var $this = this;
            return {
                bindDataGridEvent:function () {
                    $this.dataGrid.onDblClickRow = function (rowIndex, rowData) {
                        $('#title').contents()[0]['data'] = rowData['sAddress'];
                        window.frames['tabFrame'].location.href = baseUrl+'lm/rent/houseTab/'+rowData['iRHId'];
                        $this.titleRow.show();
                        $this.tabRow.show();
                        xhExtra.forwardToForm(true);
                    }
                },
                openAddForm:function () {
                    window.top.xhTop.showModal({
                        title: '新增租房信息',
                        content: '<iframe  src="'+baseUrl+'lm/rent/addForm"  style="width: 100%;;border-style: none;padding: 0px;height:400px"  scrolling="yes"   ></iframe>',
                        width:900,
                        button:'提交',
                        backClose:false
                    });
                },
                goToList:function () {
                    $this.titleRow.hide();
                    $this.tabRow.hide();
                    $this.dataGrid.reload();
                    xhExtra.backToList();

                },
                askDelQuestion:function () {
                    var id = $this.dataGrid.getSelectValues('iRHId');
                    xhExtra.askChangeStatus('lm/rent/delHouse',{id:id},null,null,function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                        $this.dataGrid.reload();
                    });
                },
                bindArea:function () {
                    $('#select_province').combobox({
                        editable:true,
                        filter: function(q, row){
                            var opts = $(this).combobox('options');
                            return row[opts.textField].indexOf(q) >= 0;
                        },
                        onSelect:function (record) {
                            if(record.value != '') {
                                $('#select_city').combobox({
                                    url: baseUrl + 'customer/customer/getAreaById/' + record.value,
                                    valueField: 'areaCode',
                                    textField: 'areaName',
                                    editable:true,
                                    filter: function(q, row){
                                        var opts = $(this).combobox('options');
                                        return row[opts.textField].indexOf(q) >= 0;
                                    },
                                    onSelect: function (record2) {
                                        if(record2.areaCode != ''){
                                            $('#select_county').combobox({
                                                url: baseUrl + 'customer/customer/getAreaById/' + record2.areaCode,
                                                valueField: 'areaCode',
                                                textField: 'areaName',
                                                editable:true,
                                                filter: function(q, row){
                                                    var opts = $(this).combobox('options');
                                                    return row[opts.textField].indexOf(q) >= 0;
                                                },
                                            })
                                        }else {
                                            $('#select_county').combobox('setValue','');
                                        }
                                    }
                                })
                            }else {
                                $('#select_city').combobox('setValue','');
                                $('#select_county').combobox('setValue','');
                            }
                        }
                    });

                    //运营部门绑定
                    $('#select_secArea').combobox({
                        onSelect:function (record) {
                            if(record.value != '') {
                                $('#select_thirdArea').combobox({
                                    url: baseUrl + 'lm/rent/findArea/' + record.value,
                                    valueField: 'id',
                                    textField: 'text'
                                })
                            }else {
                                $('#select_thirdArea').combobox('setValue','');
                            }
                        }
                    });
                },
                search:function () {
                    var key = xhExtra.getValue($('#list_search'));
                    var province  = $('#select_province').combobox('getText');
                    var city      = $('#select_city').combobox('getText');
                    var county    = $('#select_county').combobox('getText');
                    var secArea   = xhExtra.getValue($('#select_secArea'));
                    var thirdArea = xhExtra.getValue($('#select_thirdArea'));
                    var rent      = xhExtra.getValue($('#select_rent'));
                    $this.dataGrid.reload({key:key , province:province , city:city , county:county , secArea : secArea , thirdArea : thirdArea,rent:rent });
                },
                setUnRent:function () {
                    var ids = $this.dataGrid.getSelectValues('iRHId');
                    window.top.swal({
                        showCancelButton: true,
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        title: '退租理由',
                        input: 'textarea',
                        preConfirm: function(explain) {
                            return new Promise(function (resolve, reject) {
                                if(!explain){
                                    reject('说明不能为空！');
                                }
                                else {
                                    resolve(explain);
                                }
                            })
                        }
                    }).then(function (explain) {
                        xhExtra.ajaxRequest('lm/rent/unRent',{ids:ids,desc:explain},{},function (res) {
                            window.top.xhTop.showSuccess(res.msg);
                            $this.dataGrid.reload();
                        })
                    },function (dismiss) {
                        return false;
                    });
                }
            }
        }
    }
};